package com.patrol.auth.controller;

import com.patrol.auth.config.MyPasswordEncoder;
import com.patrol.auth.pojo.AdminUser;
import com.patrol.auth.service.AdminUserService;
import com.patrol.service.UserInformationService;
import com.patrol.utils.JWTUtil;
import com.patrol.utils.RegexUtil;
import com.patrol.utils.Result;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 宋灿
 * @version 1.0
 * @description: TODO
 * @date 2021/8/12 17:05
 */
@Api(value = "用户注册登录接口")
@RestController
@RequestMapping("/api")
@CrossOrigin
public class AdminUserController {
    @Resource
    private AdminUserService adminUserService;

    @Resource
    private UserInformationService userInformationService;
    @Resource
    private MyPasswordEncoder myPasswordEncoder;

    //验证账号密码
    //获取参数，账号，密码
    @ApiOperation(value = "用户注册")
    @PostMapping("/user/register")
    public Result register(@RequestParam("username") String username,
                           @RequestParam("password") String password) {

        // 判断username是否为空，该方法为空返回true
        if (StringUtil.isNullOrEmpty(username) || StringUtil.isNullOrEmpty(password)) {
            return Result.fail("用户名或者密码不能为空");
        }

        if (!RegexUtil.isLetterDigit(username)) {
            return Result.fail("用户名只能含有数字和字母");
        }

        boolean b = adminUserService.registerAdminUser(username, password);
        return b ? Result.ok() : Result.fail();
    }


    @GetMapping("/user/selectUsernameIsExist/{username}")
    public Result selectUsernameIsExist(@PathVariable String username) {
        if (StringUtil.isNullOrEmpty(username)) {
            return Result.fail("用户名不能为空！");
        }
        if (!RegexUtil.isLetterDigit(username)) {
            return Result.fail("用户名只能含有字母或者数字！");
        }
        boolean b = adminUserService.selectUsernameIsExist(username);
        return b ? Result.ok("用户名可用") : Result.fail("用户名已存在");
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/user/login")
    //获取参数，账号，密码
    public Result login(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {

        if (StringUtil.isNullOrEmpty(username) || StringUtil.isNullOrEmpty(password)) {
            return Result.fail("用户名或者密码不能为空");
        }

        if (!RegexUtil.isLetterDigit(username)) {
            return Result.fail("用户名格式只能含有数字和字母");
        }
        AdminUser adminUser = adminUserService.selectUserByUserName(username);

        if (adminUser.getUsername() == null || adminUser.getPassword() == null || !adminUser.getPassword().equals(myPasswordEncoder.md5(password))) {
            return Result.fail("用户名或密码错误");
        }
        String token = JWTUtil.sign(adminUser.getUsername(), adminUser.getPassword());
        String username1 = JWTUtil.getUsername(token);
        return Result.ok(username1);
    }

    /**
     * 在ShiroConfig中配置了退出登陆的接口路径 /api/user/logout
     */
//    @GetMapping("/user/logout")
//    public Result logout() {
//        return Result.ok();
//    }

    @GetMapping("/user/selectUserByUserName/{username}")
    public Result selectUserByUserName(@PathVariable String username) {
        if (StringUtil.isNullOrEmpty(username)) {
            return Result.ok("系统异常，获取用户名失败");
        }
        AdminUser one = adminUserService.selectUserByUserName(username);
        return null != one ? Result.ok(one) : Result.fail();
    }
}
